<?php
Class m_soal extends CI_Model
{
	public $primary_field = 'so_id';
	public $table = 'soal';

	function get_last_id(){
		$query = $this->db->query('SELECT MAX('.$this->primary_field.') as so_id FROM '.$this->table);
		$row = $query->row();
		return $row->so_id;
	}

	function get_soal_by_id($id){
		$this->db->select('*');
		$this->db -> from($this->table);
		$this -> db -> where($this->primary_field, $id);

		$query = $this->db->get();

		return $query->result_array();
	}/*

	function get_soal_with_jawaban_by_ujian($id){
	$this->db->query('
			SELECT
			soal,
			GROUP_CONCAT(jawaban ORDER BY jawaban SEPARATOR ',') jawaban
			FROM
			(
					SELECT
					CONCAT(a.so_id,'|',a.so_desc) soal,
					b.ja_desc jawaban
					FROM soal a INNER JOIN jawaban b USING (so_id)
					WHERE a.uj_id = '.$id.'
					ORDER BY a.so_id, b.ja_id
			) A
			GROUP BY soal
			');

	$query = $this->db->get();

	return $query->result();
	} */

	function get_soal_with_jawaban_by_id($id){
		$this->db->select('*');
		$this->db->from($this->table);
		$this->db->join('jawaban', 'jawaban.so_id = soal.so_id');
		$this->db->where('soal.'.$this->primary_field, $id);
		$this->db->order_by("ja_id", "asc");

		$query = $this->db->get();

		return $query->result();
	}

	function get_soal_with_correct_answer($id){
		$this->db->select('*');
		$this->db->from($this->table);
		$this->db->join('jawaban', 'jawaban.so_id = soal.so_id');
		$this->db->where('soal.'.$this->primary_field, $id);
		$this->db->where("jawaban.ja_stat", "1");

		$query = $this->db->get();

		return $query->result();
	}

	function get_soal_by_ujian($id){
		$sql = "SELECT a . * , b.ja_desc
				FROM soal a
				LEFT JOIN (
				
				SELECT so_id, ja_desc
				FROM jawaban
				WHERE ja_stat =1
				)b ON a.so_id = b.so_id
				WHERE uj_id = $id";

		$query = $this->db->query($sql);

		return $query->result();
	}

	function insert($value){
		$this->db->insert($this->table, $value);
		return mysql_insert_id();
	}

	function update($value, $where){
		return $this->db->update($this->table, $value, $where);
	}

	function delete($where){
		return $this->db->delete($this->table, $where);
	}

}
?>